package club.koorilkaze.dao.impl;

import club.koorilkaze.dao.IUserDAO;
import club.koorilkaze.vo.User;
import org.omg.CORBA.portable.ApplicationException;

import java.sql.*;
import java.util.*;

/**
 * The type User dao lmpl.
 *
 * @program: IuserDAOLmpl
 * @description: IuserDAO接口实现类, 完成具体数据库操作
 * @author: Hermit_Yoshino
 * @create: 2019 -07-24 16:29
 */
public class UserDAOLmpl implements IUserDAO {

    private Connection conn = null;
    private PreparedStatement preparedStatement = null;
    private Statement statement = null;
    private ResultSet resultSet = null;
//    private User user = new User();
    private ArrayList<User> userList = new ArrayList<User>();

    /**
     * Instantiates a new User dao lmpl.
     *
     * @param conn the conn
     */
    public UserDAOLmpl (Connection conn) {
        this.conn = conn;
    }

    @Override
    /** 
     * @Description: 实现输入 userid 查找该用户的所有信息
     * @param: [userid]
     * @author: Hermit_Yoshino
     * @date: 2019/7/24 19:21
     * @throws: Exception
     * @return: club.koorilkaze.vo.User 
     * @version: 0.0.1
     */ 
    public User selectUser (String userid) throws SQLException {

        String selectsql = "SELECT userid, name, password FROM user WHERE userid = ? ;";
        preparedStatement = conn.prepareStatement(selectsql);
        preparedStatement.setString(1, userid);

        //批处理添加
//        preparedStatement.addBatch();
        //批处理执行
//        preparedStatement.executeBatch();

        resultSet = preparedStatement.executeQuery();
        resultSet.first();
        resultSet.next();
        User user = new User();
        user.setUserid(resultSet.getString("userid"));
        user.setName(resultSet.getString("name"));
        user.setPassword(resultSet.getString("password"));

        return user;
    }

    @Override
    /** 
     * @Description: 实现输入 userid 查找该用户的所有信息接口
     * @param: []
     * @author: Hermit_Yoshino
     * @date: 2019/7/24 19:22
     * @throws: Exception
     * @return: java.util.ArrayList<club.koorilkaze.vo.User> 
     * @version: 0.0.1
     */ 
    public ArrayList<User> selectAll () throws SQLException {
        String selectall = "SELECT userid, name, password FROM user;";
        try {
            statement = conn.createStatement();
            resultSet = statement.executeQuery(selectall);
        } catch ( SQLException e) {
            System.out.println("警告：数据不完整");
            throw new SQLException("读取数据时出现SQL错误", e);
        }

//        int i = 0;
        resultSet.first();
        do {
            User user = new User();
            user.setUserid(resultSet.getString("userid"));
//            System.out.println(resultSet.getString("userid"));
            user.setName(resultSet.getString("name"));
            user.setPassword(resultSet.getString("password"));
//            System.out.print(user.getUserid() + "   ");
//            System.out.print(user.getName() + "   ");
//            System.out.println(user.getPassword());
            userList.add( user);
//            i ++;
        } while (resultSet.next());
//        for ( User user : userList ) {
//            System.out.print(user.getUserid() + "   ");
//            System.out.print(user.getName() + "   ");
//            System.out.println(user.getPassword());
//        }
        return userList;
    }

    @Override
    public void updata(ArrayList<User> list) throws SQLException{
        String upadta = "UPDATE `user` SET name = ? WHERE userid = ?;";

        try {
            conn.setAutoCommit(false);
            preparedStatement = conn.prepareStatement(upadta);
            for ( User u : list ) {
                preparedStatement.setString(1,u.getName());
                preparedStatement.setString(2,u.getUserid());
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            conn.commit();
        } catch ( SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                throw e1;
            }
            throw new RuntimeException(e);
        }
    }

}
